package cn.tpshion.vm.service.impl;

import cn.tpshion.vm.common.utils.DateUtil;
import cn.tpshion.vm.domain.dto.node.NodeImageDTO;
import cn.tpshion.vm.domain.entity.NodeImage;
import cn.tpshion.vm.domain.entity.table.NodeImageTableDef;
import cn.tpshion.vm.mapper.NodeImageMapper;
import cn.tpshion.vm.service.NodeService;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {

    @Resource
    private NodeImageMapper nodeImageMapper;

    @Override
    public List<NodeImage> imageList(String type) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        if(StringUtils.isNoneBlank(type)){
            queryWrapper.where(NodeImageTableDef.NODE_IMAGE.TYPE.eq(type));
        }
        queryWrapper.orderBy(NodeImageTableDef.NODE_IMAGE.ID.asc());
        return nodeImageMapper.selectListByQuery(queryWrapper);
    }

    @Override
    public void addImage(NodeImageDTO dto) {
        NodeImage nodeImage = new NodeImage();
        nodeImage.setType(dto.getType());
        nodeImage.setDockerImage(dto.getDockerImage());
        nodeImage.setCreateTime(DateUtil.now());
        nodeImageMapper.insert(nodeImage);
    }
}
